package com.cpj.swagger;

import com.cpj.filter.LogbackFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Created by bianxh on 2019/1/21.
 */
@Configuration
@EnableSwagger2
public class SwaggerApp  {

    @Value("${cpj.server.title:xxx服务}")
    protected String title = "xxx服务";

    @Value("${spring.application.name}")
    private String serverName;

    @Value("${cpj.server.version:1.0}")
    protected String version = "1.0";

    @Value("${cpj.owner.name:王玥麟}")
    protected String ownerName;

    @Value("${cpj.owner.email:xxx@qq.com}")
    protected String email;



    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //为当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.cpj"))
                .paths(PathSelectors.any())
                .build();
//        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build();
    }
    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description("ID:" + serverName)
                .version(version)
                .contact(new Contact(this.ownerName, "", this.email))
                .build();
    }

    @Bean
    public LogbackFilter logbackFilter(){
        return new LogbackFilter();
    }
}